﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Collections.Specialized;

namespace Microsoft.SPF.UI.WebControls
{
    [ToolboxData("<{0}:BarRangeRefinementControl runat=server></{0}:BarRangeRefinementControl>")]
    public class BarRangeRefinementControl : CompositeControl
    {
        private RefinementCollection refinementCollection;
        private RefinementCollection selectedRefinementCollection;
        private Chart rangeChartControl;
        String selectedText;

        #region Public Properties

        [Category("SPF")]
        public RefinementCollection RefinementCollection 
        {
            get
            {
                if (refinementCollection == null) refinementCollection = ViewState[UniqueID + "_refinements"] as RefinementCollection;
                if (refinementCollection == null) refinementCollection = new RefinementCollection();

                return refinementCollection;
            }
            set
            {
                ViewState[UniqueID + "_refinements"] = refinementCollection = value;
            }
        }

        [Category("SPF")]
        public RefinementCollection SelectedRefinementCollection
        {
            get
            {
                RefinementCollection rc = refinementCollection = ViewState[UniqueID + "_refinements"] as RefinementCollection;

                if (rc == null)
                {
                    selectedRefinementCollection = new RefinementCollection();
                }
                else
                {
                    //TODO: Get selected refinements
                }

                return selectedRefinementCollection;
            }
        }

        [Category("SPF")]
        public HistogramType ChartType { get; set; }

        [Category("SPF")]
        public int CropOpacity { get; set; }

        [Category("SPF")]
        public Color CropColor { get; set; }

        [Category("SPF")]
        public NameValueCollection ChartSeriesCustomProperties { get; set; }

        #endregion

        public BarRangeRefinementControl()
        {
            //デフォルトの高さ、幅
            Height = Unit.Pixel(300);
            Width = Unit.Pixel(400);
        }

        protected override void OnPreRender(EventArgs e)
        {
            //データ ポイントおよび系列の属性を設定
            Series series = CreateSeries();
            DataPoint dp;
            
            foreach (RefinementItem item in refinementCollection)
            {
                dp = new DataPoint();
                dp.Url = item.Modifier;
                dp.SetValueY(item.Value);
                dp.Label = item.Label;
                dp.MarkerImage = "bar1.jpg";
                dp.Color = System.Drawing.Color.Red ;
               

                series.Points.Add(dp);
            }

            ChartArea chartArea = new ChartArea();
            chartArea.BackColor = System.Drawing.Color.FromArgb(0, 0, 0);
            rangeChartControl.ChartAreas.Add(chartArea);

            series.LabelBackColor = System.Drawing.Color.FromArgb(0, 0, 0); 
            rangeChartControl.Series.Add(series);
            base.OnPreRender(e);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Controls.Add(rangeChartControl);
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            selectedText = Page.Request.Params[UniqueID + "_selected"];

            rangeChartControl = new Chart();
            rangeChartControl.ID = "ChartImage";
            rangeChartControl.RenderType = RenderType.ImageTag;
            rangeChartControl.IsMapEnabled = true;

            rangeChartControl.CustomizeMapAreas += new EventHandler<CustomizeMapAreasEventArgs>(rangeChartControl_CustomizeMapAreas);
        }

        void rangeChartControl_CustomizeMapAreas(object sender, CustomizeMapAreasEventArgs e)
        {
            foreach (MapArea ma in e.MapAreaItems)
            {
                ma.MapAreaAttributes = "modifier=\"" + ma.Url + "\"";
                ma.Url = string.Empty;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

           // Page.ClientScript.RegisterClientScriptResource(GetType(),
           //     "Microsoft.SPF.MDOT.js.refinementCropper.js");

           // Page.ClientScript.RegisterStartupScript(GetType(),
           //     "ChartRangeRefinementStartup_" + UniqueID, 
           //     "initdragableElements(document.getElementById(\"" + UniqueID + "\"));", true);
        }

        private void AddOpacityFilter()
        {

        }

        private void AddPoints(Series series)
        {

        }

        private Series CreateSeries()
        {
            Series series = new Series();

            switch (ChartType)
            {
                case HistogramType.Bar:
                    series.ChartType = SeriesChartType.Bar;
                    break;

                case HistogramType.Line:
                    series.ChartType = SeriesChartType.Line;
                    break;

                default:
                    series.ChartType = SeriesChartType.Column;
                    break;
            }

            return series;
        }
    }
}
